﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pavilion.Communications;

namespace Pavilion.DesignerTool
{
    static public class GameItemTransfer
    {
        static public void Add(LoadNodeWrapper node, string gameItemName)
        {
            var resourceName = node.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = node.NodeScriptWrapper.Name;

            Add(resourceName, scriptName, node.LoadNode, gameItemName);
        }

        static public void Add(string resourceName, string scriptName, LoadNode loadNode, string gameItemName)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(loadNode.ParentContainer);
            DataTransferClient.Instance.TransferData((proxy) => proxy.AddGameItem(resourceName, scriptName, containerNodeNameList, loadNode.Name, gameItemName));

            var gameToolDataCollection = loadNode.ObtainGameTools(gameItemName);

            foreach (GameData gameToolData in gameToolDataCollection)
                GameToolTransfer.Add(resourceName, scriptName, loadNode, gameToolData);
        }

        static public void Remove(LoadNodeWrapper node, string gameItemName)
        {
            var resourceName = node.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = node.NodeScriptWrapper.Name;

            Remove(resourceName, scriptName, node.LoadNode, gameItemName);
        }

        static public void Remove(string resourceName, string scriptName, LoadNode loadNode, string gameItemName)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(loadNode.ParentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.RemoveGameItem(resourceName, scriptName, containerNodeNameList, loadNode.Name, gameItemName));
        }

        static public void Rename(LoadNodeWrapper node, string oldGameItemName, string newGameItemName)
        {
            var resourceName = node.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = node.NodeScriptWrapper.Name;
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(node.ParentContainerNodeWrapper);

            DataTransferClient.Instance.TransferData((proxy) => proxy.RenameGameItem(resourceName, scriptName, containerNodeNameList, node.Name, oldGameItemName, newGameItemName));
        }
    }
}
